package cn.tedu.store.controller;

import java.util.List;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import cn.tedu.store.service.ICartService;
import cn.tedu.store.util.JsonResult;
import cn.tedu.store.vo.CartVo;

@RequestMapping("cart")
@RestController
public class CartController extends BaseController {
	@Autowired
	private ICartService service;
	
	@RequestMapping("add")
	public JsonResult<Void> addCart(Integer pid,Integer num,HttpSession session){
		//通过session获取用户 id 和username
		Integer uid = getUidFromSession(session);
		String username = getUsernameFromSession(session);
		//掉用业务用户执行增加
		service.addToCart(uid, num, pid, username);
		//响应页面
		return new JsonResult<Void>(SUCCESS);
	}
	
	@GetMapping("/")
	public JsonResult<List<CartVo>> getByUid(HttpSession session){
		//从session获取uid
		//执行查询，获取结果
		List<CartVo> data = service.getByUid(getUidFromSession(session));
		//响应成功，及查询结果
		return new JsonResult<List<CartVo>>(SUCCESS,data);
	}
	
	@RequestMapping("{cid}/add_num")
	public JsonResult<Integer> addNum(@PathVariable("cid")Integer cid,HttpSession session){
		//获取uid和username
		Integer uid = getUidFromSession(session);
		String username = getUsernameFromSession(session);
		Integer data = service.addNum(cid, uid, username);
		return new JsonResult<Integer>(SUCCESS,data);
	}
	
	@RequestMapping("get_by_cids")
	public JsonResult<List<CartVo>> getByCids(Integer[] cids,HttpSession session){
		Integer uid = getUidFromSession(session);
		List<CartVo> data = service.getByCarts(cids,uid);
		return new JsonResult<List<CartVo>>(SUCCESS,data) ;
		
	}
	
	
	
}
